/*
 * buf_CcLabel_t.c
 *
 *  Created on: 2012/04/01
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"

/*KIND_CCBUF_T*/

#define BufType  PSZ
#define Buffer ((CHAR**)stCcBufManager.memoryBlocks)
#define Buflen (stCcBufManager.maxRecordNum)
#define  bufLenPerBlk  (stCcBufManager.astMaxNum_Block)

/* get the object */
static TABLEMANAGER stCcBufManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		1,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0,
		0,
		{NULL}
};
static VOID resetBufListener( U4  procid, U2 index )
{
	if( procid == stCcBufManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			Buffer[ index-1] = NULL;
		}
	}
}

static BufType core_getBuf( AST index )
{
	BufType temp = (BufType)Check_Com( &stCcBufManager, resetBufListener, index );
	if( temp != NULL ){
		return &temp[index%bufLenPerBlk];
	}
	return NULL;
}


static BOOL AllocBuffer( AST astReqArg )
{
	return AllocBuffer_Com( &stCcBufManager, astReqArg, resetBufListener );
}
static BOOL EmptyBuffer( VOID )
{
	return AllocBuffer( - Buflen );
}
//		BOOL (*clear)( VOID );
static BOOL ClearBuffer( VOID )
{
	return Clear_Com( &stCcBufManager );
}
static BOOL ResetBuffer( VOID )
{
	return Reset_Com( &stCcBufManager );
}
static CHAR * getLabel( AST index )
{
	BufType pTbl = core_getBuf( index );
	if( pTbl == NULL )
	{
		return "";
	}
	return pTbl;
}
/* set method */
static AST  setLabel( PSZ str )
{
	S4 lenBuf = str_length(str);
	AST astHeaderNo;
	if( lenBuf > 0 && lenBuf < MemGetBlockSize( ))
	{
		if( AllocBuffer( lenBuf + 1 ) ) {
			lenBuf = Buflen - lenBuf;
			astHeaderNo = lenBuf/stCcBufManager.astMaxNum_Block;
			strcpy( &Buffer[astHeaderNo][lenBuf%stCcBufManager.astMaxNum_Block], str );
			Buffer[astHeaderNo][Buflen%stCcBufManager.astMaxNum_Block] = '\0';
		}
		else
		{
			return 0;
		}
	}
	else
	{
		if ( 0 == AllocBuffer( lenBuf + 1 ) )
		{
			return 0;
		}
		lenBuf = Buflen;
	}
	return lenBuf;
}
/* set method */
static AST  appendLabel( AST index, PSZ str )
{
	S4 lenBuf_New;
	S4 lenBuf_Old;
	AST astHeaderNo;
	PSZ pTbl = core_getBuf( index );

	lenBuf_Old = str_length(pTbl);
	if( lenBuf_Old == 0 )
	{
		return setLabel(str);
	}
	lenBuf_New = str_length(str);
	if( lenBuf_New == 0 )
	{
		return index;
	}
	if( index + lenBuf_Old >= Buflen )
	{
		if( Buflen%stCcBufManager.astMaxNum_Block + lenBuf_New < stCcBufManager.astMaxNum_Block )
		{
			if( AllocBuffer( lenBuf_New ) ) {
				lenBuf_Old = index + lenBuf_Old;
				astHeaderNo = lenBuf_Old/stCcBufManager.astMaxNum_Block;
				strcpy( &Buffer[astHeaderNo][lenBuf_Old%stCcBufManager.astMaxNum_Block], str );
				Buffer[astHeaderNo][Buflen%stCcBufManager.astMaxNum_Block] = '\0';
			}
			else
			{
				index = 0;
			}
			return index;
		}
	}
	lenBuf_New += lenBuf_Old;
	if( lenBuf_New > 0 && lenBuf_New < MemGetBlockSize( ))
	{
		if( AllocBuffer( lenBuf_New + 1 ) ) {
			lenBuf_New = Buflen - lenBuf_New;
			astHeaderNo = lenBuf_New/stCcBufManager.astMaxNum_Block;
			strcpy( &Buffer[astHeaderNo][lenBuf_New%stCcBufManager.astMaxNum_Block],
					&Buffer[index/stCcBufManager.astMaxNum_Block][index%stCcBufManager.astMaxNum_Block] );
			index = lenBuf_New;
			lenBuf_New += lenBuf_Old;
			astHeaderNo = lenBuf_New/stCcBufManager.astMaxNum_Block;
			strcpy( &Buffer[astHeaderNo][lenBuf_New%stCcBufManager.astMaxNum_Block], str );
			Buffer[astHeaderNo][Buflen%stCcBufManager.astMaxNum_Block] = '\0';
		}
		else
		{
			index = 0;
		}
	}
	else
	{
		if ( AllocBuffer( 1 ) )
		{
			index = Buflen;
		}
		else
		{
			index = 0;
		}

	}
	return index;
}
const ACCESS_CCLABEL stCcLabelStrAccess = {
		EmptyBuffer,
//		BOOL (*clear)( VOID );
		ClearBuffer,
//		BOOL (*reset)( VOID );
		ResetBuffer,
//		CHAR * (*getStr)( AST index );
		getLabel,
//		AST (*addStr)( PSZ str );
		setLabel,
//		AST (*appendStr)( AST index, PSZ str );
		appendLabel
};
